// const host = 'https://ff.xihenet.net/api/'
const app = getApp();
const host = 'https://shy.xihenet.net/'
/**
 * 封装微信的request
 * form: 'application/x-www-form-urlencoded'
 */
wx.getStorage({
  key: 'thorui_token',
  success(res) {
    //console.log(res)
    if (res.data.data != '' && res.data.data != null) {
      app.globalData.token = res.data.data
    } else {
      app.globalData.token = ''
    }
  }
})
wx.getStorage({
  key: 'uid',
  success(res) {
    //console.log(res)
    if (res.data != '' && res.data != null) {
      app.globalData.uid = res.data
    } else {
      app.globalData.uid = ''
    }
  }
})
function request(url, data = {}, method = "GET", contentType = 'json') {
  return new Promise(function(resolve, reject) {
    wx.request({
      url: host+url,
      data: data,
      method: method,
      header: {
        'Content-Type': contentType.toLowerCase() == 'json' ? "application/json" : "application/x-www-form-urlencoded",
        'token':app.globalData.token
      },
      success: function(res) {
        if(res.data.code=="40001"&&app.globalData.token!=''&&app.globalData.token!=null&&app.globalData.token!=undefined){
          wx.showModal({
            title: '温馨提示',
            content: res.data.msg+",是否退出重新登录？",
            success: function (res) {
                if (res.confirm) {
               wx.clearStorageSync();
                    wx.redirectTo({
                      url:"../../pages/shouquan/shouquan"
                    })
                } else if (res.cancel) {
                    //console.log('用户点击取消');
                }
            }
        });
        }
        if (res.statusCode == 200) {
          resolve(res.data);
        } else {
          reject(res.errMsg);
        }
      },
      fail: function(err) {
        reject(err)
      }
    })
  });
}

module.exports = {
  request: request
}